package com.lambda.demo4;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @ClassName Student
 * @Description 定义一个学生类（姓名、年龄），定义一个数组，存储若干学生，
 * 对数组各学生根据年龄降序排序，打印输出排序的结果，输出格式为：姓名-年龄
 * @Author Kx
 * @Date 2025/4/2 10:16
 * @Version 1.0
 */
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        // 定义学生数组
        Student[] students = {new Student("张三", 18), new Student("李四", 19), new Student("王五", 20)};

        // 自定义排序规则
        Comparator<Student> comparator = (s1, s2) -> {
            // 判断两个学生的年龄
//            if (s1.getAge() > s2.getAge()) {
//                return -1;
//            } else if (s1.getAge() < s2.getAge()) {
//                return 1;
//            }
//            return 0;
            return s2.getAge() - s1.getAge();
        };

        // 按照自定义排序进行排序
        Arrays.sort(students, comparator);

        // 输出排序后的结果
        for (Student student : students) {
            System.out.println(student.getName() + "-" + student.getAge());
        }
    }
}
